<?php

namespace App\HttpController;

use App\Model\ActiveJoinModel;
use App\Model\ActiveModel;
use App\Service\ActiveService;
use App\Service\SensitiveService;
use App\Utility\Verification;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="龙庭社圈-社圈活动")
 */
class Active extends BaseController
{
    /**
     * @Api(name="获取活动列表", path="/active/getList", version="1.0.0")
     * @ApiDescription(value="获取活动列表")
     * @Method(allow={POST})
     * @Param(name="is_hot", defaultValue=99, description="是否热门", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @ApiSuccess({"code": 200,"result": [{"title": "活动标题","free": "是否免费：0是，1否","price": "价格","join": "参与人数","limit": "限制人数","is_hot": "是否热门","sponsor": "主办方","matter": "注意事项","address": "活动地址","contact": "联系人","time": "活动开始时间","end_time": "活动结束时间","create_time": "活动发布时间","see": "浏览量"}],"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getList($is_hot, $limit, $page): bool
    {
        $res = ActiveService::getInstance()->getList($is_hot, $limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取活动详情", path="/active/getDetail", version="1.0.0")
     * @ApiDescription(value="获取活动详情")
     * @Method(allow={POST})
     * @Param(name="gid", required="活动id必填", description="活动id", from={JSON})
     * @ApiSuccess({"code": 200,"result": {"title": "活动标题","free": "是否免费：0是，1否","price": "价格","join": "参与人数","limit": "限制人数","is_hot": "是否热门","sponsor": "主办方","matter": "注意事项","address": "活动地址","contact": "联系人","time": "活动开始时间","end_time": "活动结束时间","create_time": "活动发布时间","see": "浏览量", "status": "参与状态：0.未参与，1.已参与"},"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDetail($gid): bool
    {
        $res = ActiveService::getInstance()->getDetail($this->uid, $gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="参与活动", path="/active/join", version="1.0.0")
     * @ApiDescription(value="参与活动")
     * @Method(allow={POST})
     * @Param(name="gid", required="活动id必填", description="活动ID", from={JSON})
     * @Param(name="num", required="活动人数必填", description="活动人数", from={JSON})
     * @Param(name="name", required="联系人必填", notEmpty="联系人不能为空",  description="联系人", from={JSON})
     * @Param(name="tel", required="联系方式必填", notEmpty="联系方式不能为空", description="联系方式", from={JSON})
     * @ApiSuccess({"code": 200, "msg": "参与成功", "data": "success"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function join($gid, $num, $name, $tel): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $checkPhone = Verification::getInstance()->is_phone($tel);
        if (!$checkPhone){
            return $this->writeJsonError('手机号输入错误');
        }
        $res = ActiveService::getInstance()->join($this->uid, $gid, $num, $name, $tel);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="参与活动-提交支付", path="/active/confirmJoin", version="1.0.0")
     * @ApiDescription(value="参与活动-提交支付")
     * @Method(allow={POST})
     * @Param(name="gid", required="活动id必填", description="活动ID", from={JSON})
     * @Param(name="price", required="报名费用必填", description="报名费用", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function confirmJoin($gid, $price): bool
    {
        $ips = $this->request()->getHeaders()['x-real-ip'] ?? [];
        $this->ip = $ips[0] ?? '127.0.0.1';
        $res = ActiveService::getInstance()->confirmJoin($this->uid, $gid, $price, $this->ip);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的活动", path="/active/getMyActive", version="1.0.0")
     * @ApiDescription(value="获取我的活动")
     * @Method(allow={POST})
     * @Param(name="status", defaultValue="99", description="状态", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getMyActive($status): bool
    {
        $res = ActiveService::getInstance()->getMyActive($this->uid, $status);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="删除我的活动", path="/active/removeMyActive", version="1.0.0")
     * @ApiDescription(value="删除我的活动")
     * @Method(allow={POST})
     * @Param(name="id", required="活动id必填", description="活动ID", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function removeMyActive($id): bool
    {
        $res = ActiveService::getInstance()->removeMyActive($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的活动详情", path="/active/getMyActiveDetail", version="1.0.0")
     * @ApiDescription(value="获取我的活动详情")
     * @Method(allow={POST})
     * @Param(name="id", required="活动id必填", description="活动ID", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyActiveDetail($id): bool
    {
        $res = ActiveService::getInstance()->getMyActiveDetail($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}